package rma.flp;

import java.util.Arrays;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;

import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 * Thin wrapper around the Spring implementation that adapts
 * the Session and Transport to the GAE infrastructure. 
 */
public class GoogleMailSenderImpl extends JavaMailSenderImpl {

	Logger logger = Logger.getLogger(GoogleMailSenderImpl.class.getName()); 

	@Override
	public synchronized Session getSession() {
		return Session.getDefaultInstance(new Properties(), null);
	}
	
	@Override
	protected Transport getTransport(Session session) throws NoSuchProviderException {
		return new Transport(session, null) {
			@Override
			public void connect(String host, int port, String username, String password) throws MessagingException {
				// Noop
			}
			@Override
			public void close() throws MessagingException {
				// Noop
			}
			@Override
			public void sendMessage(Message message, Address[] recipients) throws MessagingException {
				logger.info("Sending message '" + message.getSubject() + "' to " + Arrays.toString(recipients));
				Transport.send(message, recipients);
			}
		};
	}
	
}
